local skel = fk.CreateSkill {
  name = "emo__zhipei",
}

Fk:loadTranslationTable{
  ["emo__zhipei"] = "支配",
  [":emo__zhipei"] = "①准备阶段，或你受到伤害后，你可以令一名角色摸一张牌并横置。②每回合限一次，每当一名已横置的其他角色使用牌时，你可以为此牌重新指定目标（至少一个目标）。",

  ["#emo__zhipei-choose"] = "支配：你可以令一名角色摸一张牌并横置",
  ["#emo__zhipei_control-choose"] = "支配：你可以为 %dest 使用的%arg重新指定目标（至少1个，至多%arg2个）",
  ["#EmoChangeTargets"] = "由于 %arg 的效果，%from 使用的 %arg2 目标改为 %to",

  ["$emo__zhipei1"] = "これからあなたは私の犬です。返事は『はい』か『ワン』だけ",
  ["$emo__zhipei2"] = "と言う犬は要りません。役に立たない犬は安楽死させます",
  ["$emo__zhipei3"] = "私は人間が好き。人間が犬を好きなように",
}

skel:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "control",
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryTurn)
  end,
  can_trigger = function(self, event, target, player, data)
    return target ~= player and target.chained and player:hasSkill(skel.name)
    and player:usedEffectTimes(self.name, Player.HistoryTurn) == 0
    and data.tos and #data.tos > 0 and data.card.skill:getMinTargetNum(target) < 2
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not target:isProhibited(p, data.card) and data.card.skill:modTargetFilter(p, target, {}, data.card, {bypass_distances = true})
    end)
    if #targets == 0 then return false end
    local choose_num = math.min(#targets, #data.tos)
    local tos = room:askToChoosePlayers(player, {
      targets = targets, min_num = 1, max_num = choose_num, skill_name = skel.name, cancelable = true, no_indicate = true,
      prompt = "#emo__zhipei_control-choose::"..target.id..":"..data.card:toLogString()..":"..choose_num,
    })
    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self)
    room:doIndicate(player, {target})
    room:sendLog{ type = "#EmoChangeTargets", from = target.id,
    to = table.map(tos, Util.IdMapper),
    arg = skel.name, arg2 = data.card:toLogString()}
    room:sortByAction(tos)
    data:removeAllTargets()
    for _, to in ipairs(tos) do
      data:addTarget(to)
    end
  end,
})

local on_cost = function(self, event, target, player, data)
  local tos = player.room:askToChoosePlayers(player, {
    targets = player.room.alive_players, min_num = 1, max_num = 1, skill_name = skel.name,
    prompt = "#emo__zhipei-choose",
  })
  if #tos > 0 then
    event:setCostData(self, {tos = tos})
    return true
  end
end

local on_use = function(self, event, target, player, data)
  local to = event:getCostData(self).tos[1]
  to:drawCards(1, skel.name)
  if not to.dead and not to.chained then
    to:setChainState(true)
  end
end

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Start
  end,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_cost = on_cost,
  on_use = on_use,
})

return skel
